#include "FileListModel.h"
#include <QFileInfo>
#include <QIcon>
#include <QSet>
#include <QFileIconProvider>

#include "globalprofile.h"


FileListModel::FileListModel(QObject *parent)
  : QAbstractListModel(parent)
{
//  _dataList << "/home/charlie/a.mov"
//            << "C:\\boy\\dog.avi"
//            << "D:\\small\\a.txt";
}

int FileListModel::rowCount(const QModelIndex &parent) const
{
  // For list models only the root node (an invalid parent) should return the list's size. For all
  // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
  if (parent.isValid())
    return 0;

  return _dataList.size();
}

QVariant FileListModel::data(const QModelIndex &index, int role) const
{
  if (!index.isValid())
    return QVariant();

  if (role == Qt::DisplayRole) {
    QFileInfo f(_dataList[index.row()]);
    return f.fileName();
  }

  if (role == Qt::DecorationRole) {
    /*
    QFileInfo f (_dataList[index.row()]);
    QString e = f.suffix();
    if (pro::acceptedVideoFormats.contains(e))
    return QIcon(":/icons/movie");
    else
    return QIcon(":/icons/file");
    */
    QFileInfo f(_dataList[index.row()]);
    QFileIconProvider provider;
    QIcon icon = provider.icon(f);
    return icon;
  }

  if (role == Qt::ToolTipRole) {
    return _dataList[index.row()];
  }

  return QVariant();
}

//bool FileListModel::insertRows(int row, int count, const QModelIndex &parent)
//{
//  beginInsertRows(parent, row, row + count - 1);
//  for (int i = row; i <= row + count - 1; i++)
//    _dataList.insert(i, QString());
//  endInsertRows();
//  return true;
//}

//bool FileListModel::removeRows(int row, int count, const QModelIndex &parent)
//{

//  QStringList t;
//  for (int i = row; i <= row + count - 1; i++)
//    t << _dataList[i];
//  beginRemoveRows(parent, row, row + count - 1);
//  foreach (QString s, t)
//    _dataList.removeOne(s);
//  endRemoveRows();
//  return true;
//}


//bool FileListModel::setData(const QModelIndex &index, const QVariant &value, int role)
//{
//  if (role == Qt::EditRole) {
//    _dataList[index.row()] = value.toString();
//    emit dataChanged(index, index);
//    return true;
//  }
//  return false;
//}

void FileListModel::appendFiles(QStringList files)
{
  QStringList filteredFiles;
  foreach (QString line, files)
    if (! _dataList.contains(line))
      filteredFiles << line;

  int s = rowCount();
  int c = filteredFiles.size();
  _dataList.append(filteredFiles);
  emit dataChanged(index(s), index(s + c));
}

void FileListModel::clearAll()
{
  _dataList.clear();
  emit dataChanged(index(0), index(rowCount()));
}

QStringList FileListModel::fileList()
{
  return _dataList;
}

void FileListModel::removeByIndexes(QModelIndexList indexList, const QModelIndex &parent)
{
  QStringList filesAboutToRemove;
  foreach (QModelIndex i, indexList)
    filesAboutToRemove << _dataList[i.row()];

  removeByPaths(filesAboutToRemove, parent);

}

void FileListModel::removeByPaths(QStringList paths, const QModelIndex &parent)
{
  foreach (QString f, paths) {
    int i = _dataList.indexOf(f);
    beginRemoveRows(parent, i, i);
    _dataList.removeAt(i);
    endRemoveRows();
  }

}

void FileListModel::autoRemoveIllegalFiles()
{
  QStringList aboutToRemove;
  foreach (QString path, _dataList) {
    QFileInfo fi(path);
    if (! GlobalProfile::acceptableVideoFormats().contains(fi.suffix()))
      aboutToRemove << path;
  }

  removeByPaths(aboutToRemove);

}
